import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterapp1/ATAboutWidget.dart';
import 'package:flutterapp1/color/MyColor.dart';
import 'package:flutterapp1/color/MyUserTool.dart';
import 'package:flutterapp1/ATLoginWidget.dart';
import 'package:toast/toast.dart';
import 'package:flutterapp1/view/AppNavi.dart';

import 'Base/BaseWidget/BaseWidget.dart';
class ATMyStatefulWidget extends BaseStatefulWidget{
  ATMyStatefulWidget({Key key, this.title}) : super(key: key);
  final String title;
  @override
  State<StatefulWidget> createState() {
    return _MyState();
  }
}

class _MyState extends State<ATMyStatefulWidget> {
  var listData;
  void initState(){
    super.initState();
     loadData();
  }
  void loadData(){
    MyUserTool.login().then((success){
      if (success == true){
        listData = <String>["关于我们","用户设置","隐私设置","产品介绍","退出"];
      }else{
        listData = <String>["关于我们","用户设置","隐私设置","产品介绍","登录"];
      }
      setState(() {
      });
    });
  }
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('didChangeDependencies');
  }
  @override
  void reassemble() {
    super.reassemble();
    print('reassemble');
  }

  void openApp () async{
    //    const url = 'vnd.alipay://';// Android
//    if (await canLaunch(url)) {
//      await launch(url);
//    } else {
//      const url = 'alipay://';//iOS
//      if(await canLaunch(url)){
//        await launch(url);
//      }else{
//
//        throw 'Could not launch $url';
//      }
//    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context,width:750/2,height: 1334/2);
    return Scaffold(
    appBar: AppNavi(
        title:widget.title,
        backgroundColor: MyColor.AppColor,
        textColor: MyColor.AppFFFFFF,
        showBack: false,
    ),
      body: Center(
        child: new Column(
          children: <Widget>[
            new Container(
              height: (AppHeigh - AppStatusBar - AppNaviBar - AppToolBar),
              child: ListView.builder(
                scrollDirection: Axis.vertical,
                reverse: false,
                primary: true,
                shrinkWrap: true,
                cacheExtent: 0,
                itemExtent: 60,
                physics: new ClampingScrollPhysics(),
                itemCount: this.listData.length,
                itemBuilder: (content , index){
                  String name = this.listData[index];
                  return ListTile(title: Text(name,textAlign: (name == "退出") ? TextAlign.center : TextAlign.left,),onTap: (){
                    if (name == "退出"){
                        loginOut();
                    }else if(name == "登录"){
                      needLogin().then((success){
                        loadData();
                      });
                    }else if(name == "关于我们"){
                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (context) => new ATAboutStatefulWidget(title:"关于",)));
                    }
                  },);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
  Future <void> needLogin() async{
    await Navigator.push(
        context,
        new MaterialPageRoute(fullscreenDialog: true,
            builder: (context) => new LoginCtrl(title:"登录",)));
//    await MyUserTool.login().then((success){
//      if (success == false){
//      }
//    });
  }
  void showToast(String msg) {
    Toast.show(msg, context, duration: 2, gravity: Toast.CENTER);
  }
  void loginOut(){
    showDialog<Null>(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          width: 280,
          height: 200,
          child: new SimpleDialog(
            title: new Text('确定退出登录',textAlign: TextAlign.center,),
            children: <Widget>[
              new Container(
                child: new Row(
                  children: <Widget>[
                    new Container(
                      width:140,
                      child: SimpleDialogOption(
                        child: new Text('确定',textAlign: TextAlign.center,),
                        onPressed: () {
                          Navigator.of(context).pop();
                          MyUserTool.delete().then((success){
                            loadData();
                          });
                        },
                      ),
                    ),
                    new Container(
                      width:140,
                      child: SimpleDialogOption(

                        child: new Text('取消',textAlign: TextAlign.center,),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ),


                  ],
                ),
              ),
            ],
          ),
        );
      },
    ).then((val) {
      print(val);
    });
  }
}